package collection;

/**
 * 对Point集合进行排序
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortPoint {
    public static void main(String[] args) {
        List<Point> list = new ArrayList<>();
        list.add(new Point(1, 2));
        list.add(new Point(5, 81));
        list.add(new Point(32, 4));
        list.add(new Point(9, 68));
        list.add(new Point(4, 7));
        list.add(new Point(17, 19));

        System.out.println("list原始数据: " + list);

        Collections.sort(list, new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                //根据数轴的定义, 离原点越远的数越大, 所以需要求出距离原点的距离, 再比较, 根据勾股定理, x,y对应的是三角函数的两条边, 可以求出斜角边x^2+y^2=c^2, x和y越大,c越大, 距离圆心越远, 数也越大
                int len1 = o1.getX()*o1.getX()+o1.getY()*o1.getY();
                int len2 = o2.getX()*o2.getX()+o2.getY()*o2.getY();
                return len1 - len2;//升序排列
                //return len2 - len1;//降序排列
            }
        });
        System.out.println("list排序后: " + list);

    }
}

